﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ChenYiZh
{
    [Serializable]
    public struct NetworkMsg
    {
        public string ClientID;
        public Sign Sign;
        public byte[] Data;
        public NetworkMsg(Sign Sign, byte[] Data) {
            ClientID = Networking.ClientID;
            this.Sign = Sign;
            this.Data = Data;
        }
        public NetworkMsg(string ClientID, Sign Sign, byte[] Data)
        {
            this.ClientID = ClientID;
            this.Sign = Sign;
            this.Data = Data;
        }
        public byte[] ToByte()
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream rems = new MemoryStream();
            formatter.Serialize(rems, this);
            return rems.GetBuffer();
        }
        public static NetworkMsg ToMsg(byte[] data)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream rems = new MemoryStream(data);
            return (NetworkMsg)formatter.Deserialize(rems);
        }
    }
}
